#include<iostream>
#include <cstdio>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<pthread.h>
using namespace std;
pthread_mutex_t lock;
pthread_cond_t cond;

//function 
void *thread_run(void* arg)
{
  pthread_detach(pthread_self());
  cout<<(char*)arg<<"run..."<<endl;
  while(ture){
    pthread_cond_wait(&cond, &lock);
    cout<<"thread: "<< pthread_self()<<"act ... "<<endl;
  }
  pthread_exit((void*)10);
}

int main()
{
  pthread_mutex_init(&lock, nullptr);
  pthread_cond_init(&cond, nullptr);

  pthread_t tid1, tid2, tid3;
  pthread_create(&tid1, nullptr, thread_run, (void*)"thread 1");
  pthread_create(&tid2, nullptr, thread_run, (void*)"thread 2");
  pthread_create(&tid3, nullptr, thread_run, (void*)"thread 3");

  while(1){
    getchar();  
    pthread_cond_signal(&cond);
    //pthread_cond_broadcast(&cond);
  }

  pthread_mutex_destroy(&lock);
  pthread_cond_destroy(&cond);

  //printf("main :%lu, pid %d\n",pthread_self(),getpid());

  //sleep(10);
  //pthread_cancel(tid);
  //printf("new thread %lu be canceled!\n",tid);

  //void* ret = NULL;
  //pthread_join(tid,&ret);
  //printf("thread quit code : %d \n",(long long)ret);
  return 20;
}
